package com.smalls.redshoes.ui.widget;

import android.content.Context;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.smalls.newvideotwo.mvp.bean.MovieDetail;
import com.smalls.newvideotwo.mvp.bean.StreamInfo;
import com.smalls.redshoes.R;
import com.smalls.redshoes.util.SPUtil;
import com.smalls.redshoes.util.ViewUtils;

import java.util.ArrayList;

import cn.bingoogolapple.androidcommon.adapter.BGAAdapterViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;


public class RSMoreEpisodePopup extends MyPopup {
    private GridView gv_episode;
    private GridViewAdapter mAdapter;
    private OnEpisodeClickedListener mOnEpisodeClickedListener;
    private MovieDetail mProgramModel;
    private ArrayList<StreamInfo> mStreamingModels;
    private TextView tv_current_episode;
    private TextView tv_name;

    public interface OnEpisodeClickedListener {
        void onEpisodeClicked(int i);
    }

    class GridViewAdapter extends BGAAdapterViewAdapter<StreamInfo> {
        public GridViewAdapter(Context context) {
            super(context, R.layout.rs_item_select);
        }

        protected void fillData(BGAViewHolderHelper bgaViewHolderHelper, int position, StreamInfo streamingModel) {
            if (streamingModel != null) {
                if (!TextUtils.isEmpty(streamingModel.getSubtitle())) {
                    bgaViewHolderHelper.setText(R.id.tv_name, streamingModel.getSubtitle());
                }
                bgaViewHolderHelper.setText(R.id.tv_index, streamingModel.getSequence() + "");
            }
        }
    }

    public RSMoreEpisodePopup(Context context, View parentView) {
        super(context, parentView, ViewUtils.DipToPixels(context, 380.0f));
        initView();
        initUI();
    }

    private void initView() {
        View view = this.mParentView;
        this.mView = View.inflate(this.mContext, R.layout.rs_more_popup, null);
        initData();
    }

    private void initUI() {
        this.tv_name = (TextView) this.mView.findViewById(R.id.tv_name);
        this.tv_current_episode = (TextView) this.mView.findViewById(R.id.tv_current_episode);
        this.gv_episode = (GridView) this.mView.findViewById(R.id.gv_episode);
        this.mAdapter = new GridViewAdapter(this.mContext);
        this.gv_episode.setAdapter(this.mAdapter);
        this.gv_episode.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if (RSMoreEpisodePopup.this.mOnEpisodeClickedListener != null) {
                    RSMoreEpisodePopup.this.mOnEpisodeClickedListener.onEpisodeClicked(position);
                }
                RSMoreEpisodePopup.this.tv_name.setText("" + ((StreamInfo) RSMoreEpisodePopup.this.mStreamingModels.get(position)).getSequence());
                RSMoreEpisodePopup.this.tv_current_episode.setText(((StreamInfo) RSMoreEpisodePopup.this.mStreamingModels.get(position)).getSubtitle());
                SPUtil.putInt(RSMoreEpisodePopup.this.mContext.getApplicationContext(), RSMoreEpisodePopup.this.mProgramModel.getMedia_code(), position);
            }
        });
        this.mView.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == 61) {
                }
                return false;
            }
        });
    }

    public void changePopupWindowState() {
        this.gv_episode.requestFocus();
        this.gv_episode.setSelection(SPUtil.getInt(this.mContext, this.mProgramModel.getMedia_code(), 0));
        super.changePopupWindowState();
    }

    public void setDates(MovieDetail programModel) {
        this.mProgramModel = programModel;
        this.mStreamingModels = (ArrayList<StreamInfo>)mProgramModel.getStreaming();
        if (this.mAdapter != null) {
            this.mAdapter.setData(this.mStreamingModels);
        }
        int position = SPUtil.getInt(this.mContext.getApplicationContext(), this.mProgramModel.getMedia_code(), this.mProgramModel.getStreaming().size() - 1);
        this.tv_name.setText("" + ((StreamInfo) this.mStreamingModels.get(position)).getSequence());
        this.tv_current_episode.setText(((StreamInfo) this.mStreamingModels.get(position)).getSubtitle());
    }

    public void setOnEpisodeClickedListener(OnEpisodeClickedListener onEpisodeClickedListener) {
        this.mOnEpisodeClickedListener = onEpisodeClickedListener;
    }
}
